package com.ctgu.admin.system.controller;



import com.ctgu.admin.system.service.EmailService;
import com.ctgu.modules.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

@Controller
@RequestMapping("/email")
public class EmailController {

    @Autowired
    private EmailService emailService;

    @Autowired
    private UserService userService;

    public int makeAuthCode() {
        int authCodeNew = 0;
        authCodeNew = (int) Math.round(Math.random() * (9999 - 1000) + 1000);
        return authCodeNew;
    }



//    @RequestMapping("/sendEmail")
//    @ResponseBody
//    public boolean sendEmail(String to, String subject, String contentText, HttpSession session){
//        User user = new User();
//        user = userService.getById(Long.getLong(to));
//        contentText = "你的密码如下:"+user.getPassword() + "，请勿泄露给他人。";
//        //return  emailService.sendAttachmentMail(to,subject,contentText);
//        return true;
//    }

    @RequestMapping("/checkCode")
    @ResponseBody
    public HashMap<String,Object> checkEmail(HttpSession session,
                             @RequestParam(value = "email", required = true)String email,
                             @RequestParam(value = "code", required = true)int code){
        boolean flag = userService.checkUser(email);
        HashMap<String,Object> map = new HashMap<>();
        if(flag){
            //防止邮箱重复
            map.put("msg","该邮箱已注册");
        }else{
            if(session.getAttribute("code")==null){
                //防止前端没有传参直接调用验证码检测函数
                map.put("msg","未点击验证码发送");
                return map;
            }
            int sessionCode = (int)session.getAttribute("code");
            String sessionEmail = (String) session.getAttribute("email");
            if( session.getAttribute("code") == null || session.getAttribute("email") == null){
                //防止用户填写了邮箱又删掉了
                map.put("msg","未填写邮箱");
            }
            else if(email.equals(sessionEmail) && code == sessionCode){
                //验证码匹配成功
                map.put("msg","校验成功");
                map.put("matching","1");
            }else{
                map.put("msg","邮箱与验证码不匹配");
            }
        }
        return map;
    }


    @RequestMapping("/sendEmailCode")
    @ResponseBody
    public Object sendEmail(HttpSession session,
                             @RequestParam(value = "email", required = true)String email){
        System.out.println("Email="+email);
        boolean flag = userService.checkUser(email);
        HashMap<String,Object> map = new HashMap<>();
        if(flag){
            map.put("msg","该邮箱已注册");
        }else {
            int code = makeAuthCode();
//            int code = 1111;
            String to = email;
            String title = "技术FAN网站注册验证码";
            String content = "验证码如下:"+code;
            session.setAttribute("email",email);
            session.setAttribute("code",code);
            boolean flag1 = emailService.sendAttachmentMail(to,title,content);
            System.out.println(content);
            if(flag1)
                map.put("msg","发送成功");
            else
                map.put("msg","发送失败，检查网络或邮件地址");
        }
        return map;
    }

    public int checkSendEmail(String email) throws Exception {
        int code = 0;
        if(email==null||email.equals(""))
            throw new Exception("邮箱地址为空！");
        System.out.println("Email="+email);
        boolean flag = userService.checkUser(email);
        HashMap<String,Object> map = new HashMap<>();
        String format = "\\p{Alpha}\\w{2,15}[@][a-z0-9]{3,}[.]\\p{Lower}{2,}";
        if(!email.matches(format)){
            throw new Exception("测试错误，邮箱名格式不合法！");
        }
        if(flag){
            throw new Exception("测试错误，该邮箱在数据库中已注册");
        }else {
            code = makeAuthCode();
            String to = email;
            String title = "技术FAN网站注册验证码";
            String content = "验证码如下:"+code;
            boolean flag1 = emailService.sendAttachmentMail(to,title,content);
            System.out.println(content);
            if(flag1){
                System.out.println("测试成功");
            }else{
                throw new Exception("测试错误，邮箱地址错误");
            }
        }
        return code;
    }
}
